using System.Dynamic;
using System.Runtime;
using Godot;
using Godot.Collections;
using Ink.Parsed;

public class ActorModel : ISave
{

    public static ActorModel _instance;
    public static ActorModel getInstance()
    {
        if (_instance == null)
        {
            _instance = new ActorModel();
        }
        return _instance;
    }

    private Array<ActorHero> actorList = new Array<ActorHero>();

    private Array<ActorHero> fightHeroList = new Array<ActorHero>();
    private Array<ActorEnemy> enemyList = new Array<ActorEnemy>();

    private Array<ActorNpc> npcList = new Array<ActorNpc>();

    /*public void addHero(int actId, ActorDatas data = null)
    {

    }*/
    public Array<ActorHero> GetHeroList()
    {
        return actorList;
    }

    public Array<ActorHero> GetFightHeroList()
    {
        return fightHeroList;
    }

    public Array<ActorEnemy> GetEnemyList()
    {
        return enemyList;
    }

    public void AddHero(ActorHero hero)
    {
        bool hasActor = false;
        foreach (var actor in actorList)
        {
            if (actor.actId == hero.actId)
            {
                hasActor = true;
            }
        }

        if (!hasActor)
        {
            actorList.Add(hero);
        }
    }

    public void AddFightHero(ActorHero hero)
    {
        bool hasActor = false;
        foreach (var actor in fightHeroList)
        {
            if (actor.actId == hero.actId)
            {
                hasActor = true;
            }
        }

        if (!hasActor)
        {
            fightHeroList.Add(hero.clone());
        }
    }

    public void DeleteFightHero(ActorHero hero)
    {
        fightHeroList.Remove(hero);
        hero.QueueFree();
    }

    public bool CheckHeroInFight(ActorHero hero)
    {
        foreach (var fightHero in fightHeroList)
        {
            if (fightHero.actId == hero.actId)
            {
                return true;
            }
        }
        return false;
        //return fightHeroList.IndexOf(hero) > -1 ? true : false;
    }

    public ActorHero GetFightHeroById(int actId)
    {
        foreach (var hero in fightHeroList)
        {
            if (hero.actId == actId)
            {
                return hero;
            }
        }

        return null;
    }

    //我方角色数据
    public void Load(Godot.Collections.Dictionary<string, Variant> data)
    {
        //加载英雄数据
        ClearHeroList();

        GD.Print(data);
        Array<Variant> heroData = new Array<Variant>((Array<Variant>)data["heroData"]);
        foreach (var hero in heroData)
        {
            Godot.Collections.Dictionary<string, Variant> formatData = new Godot.Collections.Dictionary<string, Variant>((Godot.Collections.Dictionary)hero);
            ActorHero newHero = new ActorHero((int)formatData["actId"]);
            newHero.Load(formatData);

            actorList.Add(newHero);
        }

    }

    public Godot.Collections.Dictionary<string, Variant> GetSaveData()
    {

        Array<Variant> heroResult = new Array<Variant>();
        foreach (var actor in actorList)
        {
            heroResult.Add(actor.GetSaveData());
        }

        return new Godot.Collections.Dictionary<string, Variant>(){
            {"heroData",heroResult}
        };
    }

    public void ClearHeroList()
    {
        foreach (var hero in actorList)
        {
            hero.QueueFree();
        }
        actorList.Clear();
    }



}

